<?php
/**
 * 加密密码
 *
 * @param string $str 原始密码
 *
 * @return string
 */
function encryptionPwd($str)
{
    $pwdConfig = C('PWD_CONFIG');

    $pwdStr = $pwdConfig['Prefix'].$str.$pwdConfig['Suffix'];

    return md5($pwdStr);
}

/**
 * 格式化加密数据
 *
 * @param array $arr 待处理数组
 *
 * @return string
 */
function fData($arr)
{
    $str = null;
    if (empty($arr)) {
        return $str;
    }

    foreach ($arr as $k=>$v) {
        $str .=$k.':'.$v.',';
    }
echo $str;
}

/**
 * 写入日志
 *
 * @param string $dir  目录名称首字母大写
 * @param string $data 日志内容
 * @param string $type 日志类型
 *
 * @return void
 */
function writeLog($dir, $data, $type)
{
    createDir(LOG_PATH . $dir . '/');

    Log::write($data, C('LOG_LEVEL'), '', LOG_PATH . $dir . '/' . date('ymd', time()) .'_'.$type.'.log');
}

/**
 * 获取select的焦点
 *
 * @param string $str 焦点值
 *
 * @return array
 */
function getSelect($str)
{
    //返回结果
    $rs = '';

    switch($str) {
    case '0':
        $rs['s0'] = '';
        $rs['s1'] = 'selected = "selected"';
        $rs['s2'] = '';
        break;
    case '1':
        $rs['s0'] = '';
        $rs['s1'] = '';
        $rs['s2'] = 'selected = "selected"';
        break;
    default:
        $rs['s0'] = 'selected = "selected"';
        $rs['s1'] = '';
        $rs['s2'] = '';
        break;
    }

    return $rs;
}

/**
 * 图片数量焦点获取
 *
 * @param string $str 焦点值
 *
 * @return array
 */
function amountSelect($str)
{
    //初始化数组
    $rs = array();
    for ($i=0; $i<6; $i++) {
        $rs['s'.$i] = '';
    }

    if (!empty($str) and is_numeric($str)) {
        $rs['s'.$str] = 'selected = "selected"';
    } else {
        $rs['s0'] = 'selected = "selected"';
    }

    return $rs;
}

/**
 * 截取中英文混合字符串
 */
function  mixSub($str, $len, $charset="utf-8")
{
    //如果截取长度小于等于0，则返回空
    if( !is_numeric($len) or $len <= 0 )
    {
        return "";
    }

    //如果截取长度大于总字符串长度，则直接返回当前字符串
    $sLen = strlen($str);
    if( $len >= $sLen )
    {
        return $str;
    }
    //判断使用什么编码，默认为utf-8
    if ( strtolower($charset) == "utf-8" )
    {
        $len_step = 3; //如果是utf-8编码，则中文字符长度为3
    }else{
        $len_step = 2; //如果是gb2312或big5编码，则中文字符长度为2
    }

    //执行截取操作
    $len_i = 0;
    //初始化计数当前已截取的字符串个数，此值为字符串的个数值（非字节数）
    $substr_len = 0; //初始化应该要截取的总字节数

    for( $i=0; $i < $sLen; $i++ )
    {
        if ( $len_i >= $len ) break; //总截取$len个字符串后，停止循环
        //判断，如果是中文字符串，则当前总字节数加上相应编码的中文字符长度
        if( ord(substr($str,$i,1)) > 0xa0 )
        {
            $i += $len_step - 1;
            $substr_len += $len_step;
        }else{ //否则，为英文字符，加1个字节
            $substr_len ++;
        }
        $len_i ++;
    }
    $result_str = substr($str,0,$substr_len );
    return $result_str;
}

/**
 * Warning提示信息
 * @param string $type 提示类型 默认支持success, error, info
 * @param string $msg 提示信息
 * @param string $url 跳转的URL地址
 * @return void
 */
function alert($type='info', $msg='', $url='') {
    //多行URL地址支持
    $url        = $url ?str_replace(array("\n", "\r"), '', $url):$_SERVER['HTTP_REFERER'];
    $alert = unserialize(stripslashes(cookie('alert')));
    if (!empty($msg)) {
        $alert['type'] = $type;
        $alert['msg'] = $msg;
        cookie('alert', serialize($alert));
    }
    if (!empty($url)) {
        if (!headers_sent()) {
            // redirect
            header('Location: ' . $url);
            exit();
        } else {
            $str    = "<meta http-equiv='Refresh' content='0;URL={$url}'>";
            exit($str);
        }
    }

    return $alert;
}

function parseAlert() {
	$alert = unserialize(stripslashes(cookie('alert')));
	cookie('alert', null);

	return $alert;
}

/**
 * Warning提示信息
 * @param string $type 提示类型 默认支持success, error, info
 * @param string $msg 提示信息
 * @param string $url 跳转的URL地址
 * @return void
 */
function alertSuccess($msg='', $url='') {
    //多行URL地址支持
    $url        = $url ?str_replace(array("\n", "\r"), '', $url):$_SERVER['HTTP_REFERER'];
    $alert = unserialize(stripslashes(cookie('alert')));
    if (!empty($msg)) {
        $alert['type'] = 'success';
        $alert['msg'] = $msg;
        cookie('alert', serialize($alert));
    }
    if (!empty($url)) {
        if (!headers_sent()) {
            // redirect
            header('Location: ' . $url);
            exit();
        } else {
            $str    = "<meta http-equiv='Refresh' content='0;URL={$url}'>";
            exit($str);
        }
    }

    return $alert;
}

/**
 * Warning提示信息
 * @param string $type 提示类型 默认支持success, error, info
 * @param string $msg 提示信息
 * @param string $url 跳转的URL地址
 * @return void
 */
function alertError($msg='', $url='') {
    //多行URL地址支持
    $url        = $url ?str_replace(array("\n", "\r"), '', $url):$_SERVER['HTTP_REFERER'];
    $alert = unserialize(stripslashes(cookie('alert')));
    if (!empty($msg)) {
        $alert['type'] = 'error';
        $alert['msg'] = $msg;
        cookie('alert', serialize($alert));
    }
    if (!empty($url)) {
        if (!headers_sent()) {
            // redirect
            header('Location: ' . $url);
            exit();
        } else {
            $str    = "<meta http-equiv='Refresh' content='0;URL={$url}'>";
            exit($str);
        }
    }

    return $alert;
}

function getAdvertisingType(){
    return array(
        1 => '金融',
        2=> '游戏'
    );
}

function getManageType(){
    return array(
        1 =>'平台自营',
        2=>'代理经营'
    );
}

function encrypt_str($str){
    $enc = Encrypt::getInstance();
    $hashkey = "d832f531-fa80-11e3-b166-90b11c4e98ae";
    $enc->setKey($hashkey);
    return $enc->encrypt($str,false);
}

function decrypt_str($str){
    $enc = Encrypt::getInstance();
    $hashkey = "d832f531-fa80-11e3-b166-90b11c4e98ae";
    $enc->setKey($hashkey);
    return $enc->decrypt($str,false);
}

